<?php
namespace Lib\Doctrine;

class EntitySync{
    /**
     * @param \Doctrine\ORM\EntityManager $em
     * @param object $entity
     * @param mixed $data
     * @return object
     */
    public static function fromArray(\Doctrine\ORM\EntityManager $em, $entity, &$data){
        $entityClassName = get_class($entity);
        $entityMapper = $em->getClassMetadata($entityClassName);        
        $entityProperties = $entityMapper->getReflectionProperties();
        
        foreach ($entityProperties as $propertyName => $propertyValue){
            if(is_array($data)){
                if(isset ($data[$propertyName])){
                    $entityMapper->setFieldValue($entity, $propertyName, $data[$propertyName]);
                } else {
                    continue;
                }
            } elseif (is_object($data)) {
                if(isset ($data->$propertyName)){
                    $entityMapper->setFieldValue($entity, $propertyName, $data->$propertyName);
                } else {
                    continue;
                }
            } else {
                throw new EntitySyncExeption('Array or object needed');
            }
        }
        return $entity;
    }
    
    /**
     *
     * @param \Doctrine\ORM\EntityManager $em
     * @param <type> $entity
     * @param <type> $data 
     */
    public static function toArray(\Doctrine\ORM\EntityManager $em, $entity, &$data = null){
        
    }
    
    public static function toObject(\Doctrine\ORM\EntityManager $em, $entity, &$data = null){}
}

class EntitySyncExeption extends \InvalidArgumentException{}